package com.zhou.controller;

import java.util.Arrays;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhou.pojo.ArticleItem;
import com.zhou.pojo.InMsgEntity;
import com.zhou.pojo.OutMsgEntity;
import com.zhou.util.WeChatUtil;

@Controller
public class WeChatController {
	
	/*
	 * URL接入校验
	 */
	@RequestMapping(value = "weChat",method = RequestMethod.GET)
	@ResponseBody
	public String validate(String signature, String timestamp,String nonce,  String echostr) {
		//1）将token、timestamp、nonce三个参数进行字典序排序
		String[] arr = {WeChatUtil.token,timestamp,nonce}; 
		Arrays.sort(arr);
		//2）将三个参数字符串拼接成一个字符串进行sha1加密
		StringBuilder sb = new StringBuilder();
		for (String temp : arr) {
			sb.append(temp);
		}
		String mySignature = DigestUtils.shaHex(sb.toString());
		//3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
		if (mySignature.equals(signature)) {
			System.out.println("接入成功");
			return echostr;
		}else {
			System.out.println("接入失败");
			return null;
		}
		
		//请原样返回echostr参数内容，则接入生效，成为开发者成功，否则接入失败。
	}
	
	@RequestMapping(value = "weChat",method = RequestMethod.POST)
	@ResponseBody
	public Object handleMessage(@RequestBody InMsgEntity inMsgEntity){
		OutMsgEntity outMsg = new OutMsgEntity();
		//发送方
		outMsg.setFromUserName(inMsgEntity.getToUserName());
		//接收方 
		outMsg.setToUserName(inMsgEntity.getFromUserName());
		//消息创建时间
		outMsg.setCreateTime(new Date().getTime());
		//先判断inMsgEntity的消息类型
		String msgType = inMsgEntity.getMsgType();
		if ("text".equals(msgType)) {
			//消息类型
			outMsg.setMsgType("text");
			String  context = inMsgEntity.getContent();
			//设置消息内容
			if (context.contains("证爱")) {
				outMsg.setContent("加入证爱网");
			}else if (context.contains("测试")) {
				outMsg.setContent("测试回复信息");
			}else {
				outMsg.setContent("您发送了其他信息");
			}
 			
		}else if ("image".equals(msgType)) {
			outMsg.setMsgType("image");
			outMsg.setMediaId(new String[]{inMsgEntity.getMediaId()});
		}else if ("event".equals(msgType)) {
			//消息类型
			//判断是否关注事件
			if (inMsgEntity.getEvent().equals("subscribe")) {
//				outMsg.setMsgType("text");
//				outMsg.setContent("欢迎关注！！ [微笑]");
				//回复图文消息
				outMsg.setMsgType("news");
				//设置图文个数
				outMsg.setArticleCount(1);
				//设置图文明细列表
				ArticleItem item = new ArticleItem();
				item.setTitle("牵手图");
				item.setPicUrl("http://i1.umei.cc/uploads/tu/201808/9999/a46be539b2.jpg");
				item.setDescription("看着这样的爱情，就这样的相爱，不要分开好不好，"
						+ "让我也不禁想要一段可歌可泣的爱情，毕竟人这一生，总是要学会爱一回。"
						+ "爱你是一件习以为常的习惯，却让我暖到心房，你呢？一组就这样的相爱，不要分开好不好分享。");
				item.setUrl("http://study.163.com/");
				ArticleItem[] items = new ArticleItem[] {item};
				outMsg.setItem(items);
			}
		}
		return outMsg;
	}
	
	@RequestMapping("index")
	public String index(){
		return "index";
	}
}
